//
//  GameManager.m
//  SpeedTap
//
//  Created by Chi Ho Chiu on 19/8/11.
//  Copyright 2011 Individual. All rights reserved.
//

#import "GameManager.h"
#import "MainMenuScene.h"
#import "LoadingScene.h"
#import "OptionScene.h"
#import "LevelSelectScene.h"
#import "ModeSelectScene.h"
#import "LevelCompleteScene.h"
#import "GameOverScene.h"
#import "Level1Scene.h"
#import "Level2Scene.h"
#import "Level3Scene.h"

@implementation GameManager
@synthesize gameState, currentScene;
@synthesize currentLevel;


static GameManager *sharedGameManager = nil;

+(GameManager *) sharedGameManager
{

    if(sharedGameManager == nil)
    {
        sharedGameManager = [[GameManager alloc] init];
    }
    
    return sharedGameManager;
}

-(id) init
{
    if( (self = [super init]) )
    {
		[[CCDirector sharedDirector] setDeviceOrientation:CCDeviceOrientationLandscapeLeft];
        [self setCurrentLevel:-1];
        [self setGameState:kGAME_STATE_NIL];
        CCLOG(@"GameState: kGAME_STATE_NIL");
    }
    
    return self;
}

-(void) dealloc
{
    [super dealloc];
}

-(void) replaceScene:(EGameScene)kGameScene byTransition:(CCTransitionScene *)transition withDuration:(int)time
{
}

-(void) replaceScene:(EGameScene)kGameScene
{
    //Set to Portrait Mode
    //[[CCDirector sharedDirector] setDeviceOrientation:CCDeviceOrientationPortrait];
    
    //Replace by Now Loading, to reduce Memory Consumption
    [[CCDirector sharedDirector] replaceScene:[LoadingScene node]];
    CCLOG(@"Replaced Now Loading Scene");
    
    //Set the level first
    if(kGameScene >= 1 && kGameScene <= 100)
    {
        [[GameManager sharedGameManager] setCurrentLevel:kGameScene];
    }
        
    switch (kGameScene) {
        case kGAME_SCENE_MAIN_MENU:
            [[CCDirector sharedDirector] replaceScene:[MainMenuScene node]];
            CCLOG(@"Replaced MainMenuScene");
            break;
        case kGAME_SCENE_OPTION:
            [[CCDirector sharedDirector] replaceScene:[OptionScene node]];
            CCLOG(@"Replaced OptionScene");
            break;
		case kGAME_SCENE_MODE_SELECT:
			[[CCDirector sharedDirector] replaceScene:[ModeSelectScene node]];
			CCLOG(@"Replaced ModeSelectScene");
			break;
        case kGAME_SCENE_LEVEL_SELECT:
            [[CCDirector sharedDirector] replaceScene:[LevelSelectScene node]];
            CCLOG(@"Replaced LevelSelectScene");
            break;
        case kGAME_SCENE_LEVEL_COMPLETE:
            [[CCDirector sharedDirector] replaceScene:[LevelCompleteScene node]];
            CCLOG(@"Replaced LevelCompleteScene");
            break;
        case kGAME_SCENE_GAME_OVER:
            [[CCDirector sharedDirector] replaceScene:[GameOverScene node]];
            CCLOG(@"Replaced GameOverScene");
            break;
        case kGAME_SCENE_LEVEL_1:
            [[CCDirector sharedDirector] replaceScene:[Level1Scene node]];
            CCLOG(@"Replaced Level1Scene");
            break;
        case kGAME_SCENE_LEVEL_2:
            [[CCDirector sharedDirector] replaceScene:[Level2Scene node]];
            CCLOG(@"Replaced Level2Scene");
            break;
        case kGAME_SCENE_LEVEL_3:
            [[CCDirector sharedDirector] replaceScene:[Level3Scene node]];
            CCLOG(@"Replaced Level3Scene");
            break;
/*
        case kGAME_SCENE_LEVEL_4:
            [[CCDirector sharedDirector] replaceScene:[Level4Scene node]];
            CCLOG(@"Replaced Level4Scene");
            break;
        case kGAME_SCENE_LEVEL_5:
            [[CCDirector sharedDirector] replaceScene:[Level5Scene node]];
            CCLOG(@"Replaced Level5Scene");
            break;
*/
        default:
            CCLOG(@"Replacing Unknown Scene");
            break;
    }

    if(kGameScene >= 1 && kGameScene <= 100)
    {
        [self setGameState:kGAME_STATE_PLAYING];
        CCLOG(@"GameState: kGAME_STATE_PLAYING");
    }
    else
    {
        [self setGameState:kGAME_STATE_NIL];
        CCLOG(@"GameState: kGAME_STATE_NIL");
    }
    
    [self setCurrentScene:kGameScene];
}

-(void) pushScene:(EGameScene)kGameScene
{
    if(kGameScene >= 1 && kGameScene <= 100)
        [[GameManager sharedGameManager] setCurrentLevel:kGameScene];
    
    switch (kGameScene) {
        case kGAME_SCENE_MAIN_MENU:
            [[CCDirector sharedDirector] pushScene:[MainMenuScene node]];
            CCLOG(@"Pushed MainMenuScene");
            break;
        case kGAME_SCENE_OPTION:
            [[CCDirector sharedDirector] pushScene:[OptionScene node]];
            CCLOG(@"Pushed OptionScene");
            break;
        case kGAME_SCENE_LEVEL_SELECT:
            [[CCDirector sharedDirector] pushScene:[LevelSelectScene node]];
            CCLOG(@"Pushed LevelSelectScene");
            break;
        case kGAME_SCENE_LEVEL_1:
            [[CCDirector sharedDirector] pushScene:[Level1Scene node]];
            CCLOG(@"Pushed Level1Scene");
            break;
        default:
            CCLOG(@"Pushing Unknown Scene");
            break;
    } 

    [self setCurrentScene:kGameScene];
}

-(void) popScene
{
    [[CCDirector sharedDirector] popScene];
}

@end
